package cappuccino.couldbook.ui;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Toast;

import java.util.List;

import cappuccino.couldbook.R;
import cappuccino.couldbook.base.BaseActivity;
import cappuccino.couldbook.core.mananger.AccountM;
import cappuccino.couldbook.data.PhoneNumber;
import cappuccino.couldbook.data.Result;
import cappuccino.couldbook.net.NetApi;
import cappuccino.couldbook.net.Network;
import cappuccino.couldbook.ui.Adapter.RecylerNumbersAdapter;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/9/5.
 */
public class QueryNumberActivity extends BaseActivity {

    String TAG = "QueryNumberActivity";

    @Override
    protected void init() {

    }

    @Override
    protected void onStart() {
        super.onStart();
        showBack();
    }

    RecyclerView rv;
    int type;
    @Override
    protected void bindView() {
        setContentView(R.layout.activity_recylerview);
        rv = (RecyclerView)findViewById(R.id.rv_rv);
        type = getIntent().getIntExtra("type",0);
        int userId = getIntent().getIntExtra("userId",-1);
        Log.v(TAG,"userId:"+userId);
        if(userId!=-1)
            query(userId);
    }

    Observer<Result<List<PhoneNumber>>> observer = new Observer<Result<List<PhoneNumber>>>() {

        @Override
        public void onCompleted() {
            Log.v(TAG, "onCompleted");
        }

        @Override
        public void onError(Throwable throwable) {
            Log.v(TAG, "onError");
            Toast.makeText(QueryNumberActivity.this.getApplicationContext(), "We meet some problem ", Toast.LENGTH_SHORT).show();
            Log.v(TAG,throwable.toString());
        }

        @Override
        public void onNext(Result<List<PhoneNumber>> res) {
            Log.v(TAG, "OnNext");
            if(res.status == 1){
                rv.setAdapter(new RecylerNumbersAdapter(QueryNumberActivity.this,res.data,type));
                rv.setLayoutManager(new LinearLayoutManager(QueryNumberActivity.this));
            }
            else
                Toast.makeText(QueryNumberActivity.this.getApplicationContext(),"Fail:"+res.message,Toast.LENGTH_SHORT).show();
        }
    };
    private void query(int userId){
        NetApi api = Network.getNetApi();
        AccountM accountM = new AccountM(this);
        int id = accountM.getAccout();
        if(id == -1)
        {
            Toast.makeText(this,"请先登录",Toast.LENGTH_SHORT).show();
        }else{
            api.querynumbers(userId).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(observer);
        }
    }

}
